<?php
namespace app\dboard\controller;
use think\captcha;
use think\facade\Cookie;
use think\facade\Session;
use think\facade\Url;
use think\Db;
class Ashop extends Base
{
    public function initialize() {
        parent::initialize();
        $returnUrl=Url::build('dboard/alogin/sign');
        if(!isset($this->AdminAccount['AdminID']) || intval($this->AdminAccount['AdminID'])<=0 || !isset($this->AdminAccount['PAdminID'])){
            if($this->request->isAjax()){
                return $this->print_result(['Code'=>9999,'Msg'=>'请登录']);
            }
            return $this->redirect($returnUrl);
        }
    }
    public function add_shop(){
        $input = $this->request->param(false);
        $id = isset($input['sid'])?$input['sid']:0;
        $store = ['add_teacher'=>'','is_group'=>''];
        if ($id) {
            $store = Db::name('store')->where(['id'=>$id])->find();
        }
        $this->assign('store',$store);
        return view('ashop/add_shop');
    }
    public function shopLists(){
        return view('ashop/lists');
    }
   public function doCreateStore(){
        if (!$this->request->isAjax()) {
            $this->res_data['Msg'] = '请求方式错误';
            return $this->print_result($result);
        }
        $param    = $this->request->param(false);
        $ashop= Controller('Ashop', 'logic');
        $result            = $ashop->create($param);
        return $this->print_result($result);
   }
    public function ajaxList(){
        if (!$this->request->isAjax()) {
            $this->res_data['Msg'] = '请求方式错误';
            return $this->print_result($this->res_data);
        }
        $input = $this->request->param(false);
        $page_limit      = isset($input['length'])?$input['length']:10;
        $page_start      = isset($input['start'])?$input['start']:0;
        $p = 1;
        if ($page_start > 0) {
            $p = ceil($page_start / ($page_limit - 1));
        }
        $where = [];
        $count=Db::name('store')->where($where)->count();
        $list  = Db::name('store')
              ->where($where)
              ->limit($page_start, $page_limit)->select();
        foreach($list as &$v){
            $v['create_date'] = date('Y-m-d H:i',$v['createtime']);
            $v['start_time'] = $v['start_time']==0?'-':date('Y-m-d H:i',$v['start_time']);
            $v['end_time'] = $v['end_time']==0?'-':date('Y-m-d H:i',$v['end_time']);
        }
        
        $this->res_data['Msg']='OK';
        $this->res_data['Code']=0;
        $this->res_data['Data']=['store'=>$list,'Data'=>$list,'recordsTotal'=>$count];
        $this->res_data['recordsTotal']=$count;
        return $this->print_result($this->res_data);
   }
   public function edit(){
        if (!$this->request->isAjax()) {
            $this->res_data['Msg'] = '请求方式错误';
            return $this->print_result($result);
        }
        $param    = $this->request->param(false);
        $ashop= Controller('Ashop', 'logic');
        $result            = $ashop->edit($param);
        return $this->print_result($result);
  }
   public function info(){
        if (!$this->request->isAjax()) {
            $this->res_data['Msg'] = '请求方式错误';
            return $this->print_result($result);
        }
        $param    = $this->request->param(false);
        $ashop= Controller('Ashop', 'logic');
        $result            = $ashop->storeInfo($param);
        return $this->print_result($result);
   }
   public function updateStore(){
        if (!$this->request->isAjax()) {
            $this->res_data['Msg'] = '请求方式错误';
            return $this->print_result($result);
        }
        $param    = $this->request->param(false);
        $ashop= Controller('Ashop', 'logic');
        $result            = $ashop->updateStore($param);
        return $this->print_result($result);
   }
    public function edit_pwd(){
        return view('ashop/edit_pwd');
    }
   public function editAdminPwd(){
        if (!$this->request->isAjax()) {
            $this->res_data['Msg'] = '请求方式错误';
            return $this->print_result($result);
        }
        $param    = $this->request->param(false);
        
        $id = isset($param['id']) ? trim($param['id']):'';
        $oldpwd = isset($param['oldpwd']) ? trim($param['oldpwd']):'';
        $newpwd = isset($param['newpwd']) ? trim($param['newpwd']):'';
        $newpwd_confirm = isset($param['newpwd_confirm']) ? trim($param['newpwd_confirm']):'';
        if (!$id || !$oldpwd || !$newpwd || !$newpwd_confirm ) {
            $this->res_data['Msg'] = "缺少必填信息";
            return $this->print_result($this->res_data);
        }
        if ($newpwd != $newpwd_confirm ) {
            $this->res_data['Msg'] = "新密码两次输入不一致";
            return $this->print_result($this->res_data);
        }
        $password=get_admin_pwd($oldpwd);
        $teacher= Db::name('admin_user')->where([['id','=',$id],['password','=',$password]])->find();
        if (!$teacher) {
            $this->res_data['Msg'] = "旧密码错误";
            return $this->print_result($this->res_data);
        }
    
        $data['password'] = get_admin_pwd($newpwd);
        $data['updatetime'] = time();
        $data['token'] = '';
        $sdata = Db::name('admin_user')->where([['id','=',$id]])->data($data)->update();
        if(!$sdata) {
            //Db::rollback();
            $this->res_data['Msg'] = '编辑失败';
            return $this->print_result($this->res_data);
        }

        $this->res_data['Msg'] = '成功';
        $this->res_data['Code'] = 0;
        return $this->print_result($this->res_data);
   }
}
